package com.spark.mooc.ch2_scala.part02_objectOrientedProgramming

/**
 * @description:
 * @time: 2020/11/24 18:15
 * @author: lhy
 */
/**
 * 特质：
 * 1.特质里面可以有抽象方法，也可以有具体方法
 * 2.特质定义抽象成员不需要使用关键字abstract
 * 3.特质可以继承其它特质
 * 4.特质混入类中
 *   4.1 with
 *   4.2 extends
 * 5.scala中一个类继承几个超类，
 * 但是可以混入多个特质，重用特质中的一些方法字段，从而间接实现多继承的效果
 */
trait Flyable{
    var maxFlyHeight:Int    //抽象字段
    def fly()               //抽象方法
    def breathe(): Unit ={  //具体方法
        println("I can breath")
    }
}
trait HasLegs{
    val legs:Int
    def move(): Unit =printf("I can walk with %d legs\n",legs)
}

class Animal(val category:String){
    def info(): Unit = println("This is a "+category)
}
class Bird(flyHeight:Int) extends Flyable{
    override var maxFlyHeight: Int = flyHeight
    override def fly(): Unit = printf("I can fly at the height of %d\n",maxFlyHeight)
}
class BigBird(flyHeight:Int) extends Animal("BigBird") with Flyable with HasLegs{
    override var maxFlyHeight: Int = flyHeight
    override val legs: Int = 2
    override def fly(): Unit = printf("I can fly at the height of %d\n",maxFlyHeight)

}

object TestTrait {
    def main(args: Array[String]): Unit = {
        val bird = new Bird(100)
        bird.fly()
        bird.breathe()
        val bigBird = new BigBird(200)
        bigBird.info()
        bigBird.fly()
        bigBird.breathe()
        bigBird.move()

    }
}
